package cn.yunhe.service.impl;

import cn.yunhe.dao.QuestionDao;
import cn.yunhe.entity.Field;
import cn.yunhe.entity.Question;
import cn.yunhe.service.QuestionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class QuestionServiceImpl implements QuestionService {

    @Resource
    QuestionDao questionDao;



    @Override
    public int getQuestionCount() {
        return questionDao.getQuestionCount();
    }

    /**
     * 获取根据科目分类试题统计
     * @return
     * 郭瀚钰
     */
    @Override
    public List<Field> getQuestionCountGroupByFieldId() {
        List<Field> questionCountByFieldList =  questionDao.getQuestionCountGroupByFieldId();
        int totalQuestionCount = questionDao.getQuestionCount();
        for (Field ql:questionCountByFieldList){
            ql.setQuestionCount(ql.getQuestionCount()*100/totalQuestionCount);
        }
        return questionCountByFieldList;
    }

    @Override
    public boolean addQuestion(Question question) {

        boolean flag=questionDao.addQuestion(question);
        return flag;
    }


    @Override
    public boolean upd(Question question) {
        boolean flag=questionDao.upd(question);
        return flag;
}

    @Override
    public boolean delQuestionPoint(Integer questionId) {
        boolean flag=questionDao.delQuestionPoint(questionId);
        return flag;
    }

    @Override
    public boolean addQuestionPoint(Integer questionId, Integer pointId) {
        boolean flag=questionDao.addQuestionPoint(questionId,pointId);
        return flag;
    }

    @Override
    public List<Question> queryQuestion(Question question) {

        List<Question> list= questionDao.queryQuestion(question);
        return list;
    }

    @Override
    public Integer queryQuestionCount(Question question) {

           Integer count= questionDao.queryQuestionCount(question);
        return count;
    }

    @Override
    public Page queryQuestion(Question question , Integer currPage) {
        Integer count=queryQuestionCount(question);
        //System.out.println("count==="+count);
        Page page = new Page();
        /**
         * 总条数
         */
        page.setTotal(count);
        int totalPage = (count/3) + (count % 10 == 0 ? 0 : 1);
        /**
         * 总页数
         */
        page.setPageNum(totalPage);
        page.setPageSize(10);
        page = PageHelper.startPage(currPage,10);
        List<Question> list= questionDao.queryQuestion(question);
        return page;
    }
    /**
     * 批量查询试题详情
     * @return 试题集
     */
    public List<Question> queryQuestionByList(List<Question> list){

       return questionDao.queryQuestionByList(list);

    };

    /**
     *查询试题详情
     * @return 试题
     */
    public Question queryQuestionById(Integer id){

         return questionDao.queryQuestionById(id);
    }


}
